package com.example.springboot.controller;

import com.example.springboot.common.result.Result;
import com.example.springboot.common.request.UserListRequest;
import com.example.springboot.entity.User;
import com.example.springboot.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 批量删除用户信息
     * @param userIds
     * @return
     */
    @DeleteMapping("/deleteUsers/{userIds}")
    public Result deleteBatch(@PathVariable("userIds") Integer[] userIds) {
        userService.deleteBatch(userIds);
        return Result.success();
    }

    /**
     * 查询所有用户信息
     * @return
     */
    @GetMapping("/list")
    public Result list() {
        return Result.success(userService.list());
    }

    /**
     * 分页模糊查询
     * @param userListRequest
     * @return
     */
    @GetMapping("/page")
    public Result page(UserListRequest userListRequest) {
        return Result.success(userService.page(userListRequest));
    }

    /**
     * 新增用户
     * @param user
     * @return
     */
    @PostMapping("/save")
    public Result save(@RequestBody User user) {
        userService.save(user);
        return Result.success();
    }

    /**
     * 根据id获取需要修改的用户信息
     * @param id
     * @return
     */
    @GetMapping("/update/{id}")
    public Result getById(@PathVariable Integer id) {
        User user = userService.getById(id);
        return Result.success(user);
    }

    /**
     * 修改用户信息
     * @param user
     * @return
     */
    @PutMapping("/update")
    public Result editUser(@RequestBody User user) {
        userService.update(user);
        return Result.success();
    }

    /**
     * 根据id删除用户
     * @param id
     * @return
     */
    @DeleteMapping("/delete/{id}")
    public Result deleteById(@PathVariable Integer id) {
        userService.deleteById(id);
        return Result.success();
    }
}
